import { useAppSelector } from '@/redux/store';
import { useRequest } from 'ahooks';
import type { SelectProps } from 'antd';
import { Select } from 'antd';
import { FC, useState } from 'react';
import { getAssociationTreeData } from './data';

type Option = {
  label: string;
  value: string;
};

export type StudyStatisticsSelectProps = SelectProps;

const StudyStatisticsSelect: FC<any> = ({ ...rest }) => {
  const { entId } = useAppSelector((state) => state.loginEnt);
  const [options, setOptions] = useState<Option[]>([
    {
      label: '自建',
      value: entId,
    },
  ]);

  useRequest(
    () => {
      return getAssociationTreeData({
        entId,
        type: 1,
        count: false,
      });
    },
    {
      onSuccess(data) {
        if (data.list.length > 0) {
          const newOptions = data.list.map((enterprise) => {
            return {
              label: enterprise.name,
              value: enterprise.entId,
            };
          });

          setOptions([...options, ...newOptions]);
        }
      },
    },
  );

  return (
    <>
      <Select {...rest} variant="borderless" style={{ width: 200, marginLeft: '10px' }} options={options} />
    </>
  );
};

export default StudyStatisticsSelect;
